package com.gitee.zycra.future.job.web.controller;

import com.gitee.zycra.future.job.common.bo.CommonResult;
import com.gitee.zycra.future.job.common.param.AddScheduleParam;
import com.gitee.zycra.future.job.common.param.UpdateScheduleParam;
import com.gitee.zycra.future.job.common.vo.JobScheduleNameVO;
import com.gitee.zycra.future.job.common.vo.JobScheduleVO;
import com.gitee.zycra.future.job.service.JobScheduleService;
import com.gitee.zycra.future.job.web.annotation.AuthCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zycra
 */
@Slf4j
@RestController
@RequestMapping("/schedule")
public class JobScheduleController {

    @Autowired
    private JobScheduleService jobScheduleService;

    /**
     * 添加
     *
     * @param param 执行信息对象
     * @return 添加结果
     */
    @AuthCheck({"6", "7"})
    @PostMapping("/addJobSchedule")
    public CommonResult<String> addJobSchedule(@RequestBody @Validated AddScheduleParam param) {
        if (param.getScheduleParam() == null || param.getScheduleParam().isBlank()) {
            param.setScheduleParam("{}");
        }
        return jobScheduleService.insertJobSchedule(param);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 删除结果
     */
    @AuthCheck({"6", "7"})
    @PostMapping("/deleteJobSchedule")
    public CommonResult<String> deleteJobSchedule(@RequestParam("id") Long id) {
        return jobScheduleService.deleteJobSchedule(id);
    }

    /**
     * 更新
     *
     * @param param 执行信息对象
     * @return 更新结果
     */
    @AuthCheck({"6", "7"})
    @PostMapping("/updateJobSchedule")
    public CommonResult<String> updateJobSchedule(@RequestBody @Validated UpdateScheduleParam param) {
        if (param.getScheduleParam() == null || param.getScheduleParam().isBlank()) {
            param.setScheduleParam("{}");
        }
        return jobScheduleService.updateJobSchedule(param);
    }

    /**
     * 切换开关
     *
     * @param id     主键
     * @param enable 是否开启
     * @return 切换结果
     */
    @AuthCheck({"6", "7"})
    @PostMapping("/toggleEnable")
    public CommonResult<String> toggleEnable(@RequestParam("id") Long id, @RequestParam("enable") Boolean enable) {
        return jobScheduleService.toggleEnable(id, enable);
    }

    /**
     * 根据元数据ID查询
     *
     * @param jobMetaDataId 元数据ID
     * @return 查询结果
     */
    @AuthCheck({"4", "5", "6", "7"})
    @PostMapping("/queryJobSchedule")
    public CommonResult<List<JobScheduleVO>> queryJobSchedule(@RequestParam("jobMetaDataId") Long jobMetaDataId) {
        List<JobScheduleVO> result = jobScheduleService.queryJobSchedule(jobMetaDataId);
        return CommonResult.success(result);
    }

    /**
     * 查询所有任务信息
     *
     * @param jobCycle          执行周期
     * @param currentScheduleId 当前调度信息ID
     * @return 所有任务信息
     */
    @AuthCheck({"6", "7"})
    @GetMapping("/queryAllSchedule")
    public CommonResult<List<JobScheduleNameVO>> queryAllSchedule(@RequestParam("jobCycle") Integer jobCycle, @RequestParam(value =
            "currentScheduleId", required = false) Long currentScheduleId) {
        List<JobScheduleNameVO> result = jobScheduleService.queryAllSchedule(jobCycle, currentScheduleId);
        return CommonResult.success(result);
    }
}
